package com.usian.order.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.usian.jyzx.model.entity.order.OrderInfo;
import com.usian.jyzx.model.entity.order.OrderItem;
import com.usian.order.mapper.OrderInfoMapper;
import com.usian.order.mapper.OrderItemMapper;
import com.usian.order.service.OrderInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @TODO:
 * @Author:baimu
 * @Date:2024 07 16 09 04
 **/
@Service
public class OrderInfoServiceImpl implements OrderInfoService {
    @Autowired(required = false)
    OrderInfoMapper orderInfoMapper;

    @Autowired(required = false)
    OrderItemMapper orderItemMapper;

    @Override
    public int save(OrderInfo orderInfo) {
        return orderInfoMapper.save(orderInfo);
    }

    @Override
    public OrderInfo findOrderInfoByOrderId(String orderId) {
        return orderInfoMapper.findOrderInfoByOrderId(orderId);
    }

    @Override
    public PageInfo<OrderInfo> findOrderByUserIdAndOrderStatus(
            Integer pageNum, Integer pageSize,
            Integer  userId, Integer status) {
        PageHelper.startPage(pageNum, pageSize);
        List<OrderInfo> orderInfoList = orderInfoMapper.findOrderByUserIdAndOrderStatus(userId, status);
        orderInfoList.forEach(orderInfo -> {
            String orderNo = orderInfo.getOrderNo();
            List<OrderItem> orderItemList = orderItemMapper.findOrderItemByOrderId(orderNo);
            orderInfo.setOrderItemList(orderItemList);
            //orderInfo.setId(Integer.valueOf(orderNo));

        });

        PageInfo<OrderInfo> orderInfoPageInfo = new PageInfo<>(orderInfoList);

        return orderInfoPageInfo;
    }
}
